package com.kaiwukj.android.ufamily.mvp.ui.page.home.message.add;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ChatSearchGroupResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ChatSearchPersonResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.message.MessageContract;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

public class FriendAddPresenter extends BasePresenter<MessageContract.Model, MessageContract.SearchView> {

    public static final int ATTENTION = 1;
    public static final int ATTENTION_CANCEL = 2;

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public FriendAddPresenter(MessageContract.Model model, MessageContract.SearchView view) {
        super(model, view);
    }

    public void searchPerson(String keyword) {
        mModel.searchChatPersonResult(keyword)
                .doOnSubscribe(act -> mRootView.showLoading("正在搜索..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<List<ChatSearchPersonResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<ChatSearchPersonResult> result) {
                        mRootView.onPersonResult(result);
                    }
                });
    }

    public void searchGroup(String keyword) {
        mModel.searchChatGroupResult(keyword)
                .doOnSubscribe(act -> mRootView.showLoading("正在搜索..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<List<ChatSearchGroupResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<ChatSearchGroupResult> result) {
                        mRootView.onGroupResult(result);
                    }
                });
    }

    public void postAttention(int userId) {
        mModel.attention(userId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ATTENTION) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("关注成功");
                    }
                });
    }

    public void postCancelAttention(int userId) {
        mModel.cancelAttention(userId)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, ATTENTION_CANCEL) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("取消成功");
                    }
                });
    }


}
